package com.克隆02.方式02;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;

/**
 * 深克隆
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {

    private int age;
    private String name;

    public static void main(String[] args) throws Exception{

        Person person = new Person(1002,"传智");

        //通过流的方式写对象
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(person);
        oos.flush();
        //通过流的方式读取对象，克隆对象
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Person clonePerson  = (Person) ois.readObject();//此处实现了克隆

        clonePerson.setName("黑马");
        System.out.println(person==clonePerson);
        System.out.println(person.getName());
        System.out.println(clonePerson.getName());

    }

}
